﻿#region Using Directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using System;
using System.Text;
using System.Collections.Generic;

using Xeno_Race.Helpers;
using Xeno_Race.Graphics;
using Xeno_Race.SoundManager;
#endregion

namespace Xeno_Race.Game
{
    public class Player
    {
        /// <summary>
        /// Player name
        /// </summary>
        public String Name
        {
            get { return name; }
            set { name = value; }
        }
        private String name;

        /// <summary>
        /// Selected ship
        /// </summary>
        public int ShipSelection
        {
            get { return selectedShip; }
            set { selectedShip = value; }
        }
        private int selectedShip = 0;

        /// <summary>
        /// Selected track
        /// </summary>
        public int TrackSelection
        {
            get { return selectedTrack; }
            set { selectedTrack = value; }
        }
        private int selectedTrack = 0;

        #region Lap variable
        /// <summary>
        /// Remember all lap times for the victory screen.
        /// </summary>
        private List<double> lapTimes = new List<double>();

        /// <summary>
        /// The number of laps in each race
        /// </summary>
        public int LapCount = 3;

        /// <summary>
        /// Constructor
        /// </summary>
        public Player(String name)
        {
            this.name = name;
        }

        /// <summary>
        /// Add lap time
        /// </summary>
        /// <param name="setLapTime">Lap time</param>
        public void AddLapTime(double setLapTime)
        {
            lapTimes.Add(setLapTime);
        }

        /// <summary>
        /// Current round time
        /// </summary>
        public double PlayingTime { get; set; }

        public void OnLapCompleted(object sender, EventArgs e)
        {
            AddLapTime(PlayingTime);
            PlayingTime = 0;
        }

        #endregion

        #region Reset
        /// <summary>
        /// Reset player values.
        /// </summary>
        public void Reset()
        {
            lapTimes.Clear();
            PlayingTime = 0;
        }
        #endregion

        /// <summary>
        /// Returns an array with all completed lap times
        /// </summary>
        public String[] getLaptimes()
        {
            List<String> laptimes = new List<String>();
            for (int num = 0; num < lapTimes.Count; num++)
            {
                laptimes.Add(formatTime(lapTimes[num]));
            }

            return laptimes.ToArray();
        }

        /// <summary>
        /// Returns current round time
        /// </summary>
        public String getCurrentTime()
        {
            return formatTime(PlayingTime);
        }

        /// <summary>
        /// Converts time in milliseconds to a formated string MM:SS:ms
        /// </summary>
        private String formatTime(double time)
        {
            TimeSpan intervall = new TimeSpan();
            intervall = TimeSpan.FromMilliseconds(time);

            return intervall.Minutes.ToString("00") +":" +
                   intervall.Seconds.ToString("00") +":" +
                   intervall.Milliseconds.ToString("00");
        }
    }
}
